﻿///<reference path="~/Scripts/-vsdoc/jquery-1.3.2-vsdoc.js"/>
///<reference path="~/Scripts/-vsdoc/jquery.validate-vsdoc.js"/>
///<reference path="~/Scripts/-vsdoc/console-vsdoc.js"/>

$.fn.setValidation = function(options)
{
    var validator = $('form').validate(options);

    $(validator.currentForm).find(":text, :textarea, :checkbox").blur(function()
    {
        $(this).valid();
    });

    $(validator.currentForm).find(":radio").focus(function()
    {
        this._hasFocus = true;
    }).blur(function()
    {
        this._hasFocus = false;
        var radio = this;
        var radioGroup = $('[name=' + radio.name + ']');
        var hasError = false;

        radioGroup.each(function()
        {
            return !(hasError = $(this).hasClass(validator.settings.errorClass))
        });

        if (!hasError)
            setTimeout(function validateRadioGroup()
            {
                var radioGroupHasFocus = false;

                radioGroup.each(function()
                {
                    return !(radioGroupHasFocus = this._hasFocus);
                });

                if (!radioGroupHasFocus)
                    $(radioGroup).valid();
            }, 50);
    });
};